<template>
  <div :style="{height:height,width:width}" />
</template>

<script>
import * as echarts from 'echarts';
require('echarts/theme/macarons')
import resize from './mixins/resize'

const animationDuration = 3000

export default {
  mixins: [resize],
  props: {
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '100%'
    },
    backupState: {
      type: Array,
      default:()=>{
        return []
      }
    },
  },
  data() {
    return {
      chart: null,
      series:[
        {
          type: 'bar',
          // itemStyle: {
          //   color:function(params){
          //     var colorList  = ['#6698FF','#1F45FC','#82CAFF','#43BFC7','#C36241']
          //     return colorList[params.dataIndex];
          //   },
          // },
          label: {
            show: true,
            position: 'inside',
            fontSize: 14,
            color:'#fff',
          },
          barWidth: '50%',
          data: [],
          animationDuration
        }
      ],
      yAxis:[
        {
          type: 'category',
          data: [],
          axisLine:{
            show: false,
            lineStyle:{
              color:'#B6B6B4',
              type:'dashed'
            }
          },
          splitLine: { show: false },
          axisTick: {
            show:false,
            alignWithLabel: true
          },
          axisLabel: {
            formatter: '{value}'
          }
        }
      ]
    }
  },
  mounted() {
    this.getData()
    this.$nextTick(() => {
      this.initChart()
    })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    getData(){
      if (this.backupState && this.backupState.length){
        this.series[0].data = this.backupState.map(val=>val.warehouseTotalCount)
        this.yAxis[0].data = this.backupState.map(item => item.backupName)
      }
    },
    selectClick(str,status){
      this.yAxis[0].data = ['CMX仓库', 'CEP仓库','CCA仓库']
      this.series[0].data = [88, 47, 356]
      switch (str) {
        case 'CMX仓库':
          if (status){
            this.yAxis[0].data = ['CEP仓库','CCA仓库']
            this.series[0].data = this.series[0].data.splice(1,2)
          }
          break;
        case 'CEP仓库':
          if (status){
            this.yAxis[0].data = ['CMX仓库','CCA仓库']
            this.series[0].data = this.series[0].data.splice(1,2)
          }
          break;
        case 'CCA仓库':
          if (status){
            this.yAxis[0].data = ['CMX仓库', 'CEP仓库']
            this.series[0].data = this.series[0].data.splice(0,2)
          }
          break;
      }
      this.chart.setOption({
        series:this.series,
        yAxis:this.yAxis
      })
    },
    initChart() {
      this.chart = echarts.init(this.$el, 'macarons')
      this.chart.setOption({
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'shadow'
          },
          formatter: '{b}: {c}',
          textStyle:{
            color:'#fff'
          }
        },
        legend: {
          show:false,
          data: ['CMX仓库', 'CEP仓库','CCA仓库']
        },
        grid: {
          top: 30,
          left: '1%',
          right: '3%',
          bottom: '3%',
          containLabel: true
        },
        xAxis: [{
          type: 'value',
          position: 'top',
          splitLine: {
            lineStyle: {
              type: 'dashed'
            }
          },
          axisLabel: {
            formatter: '{value}'
          },
          boundaryGap: [0, 0.01],
          axisLine:{
            lineStyle:{
              color:'#B6B6B4'
            }
          },
          axisTick: {
            alignWithLabel: true
          }
        }],
        yAxis:this.yAxis,
        series:this.series
      })
    }
  }
}
</script>
